package lp2.projeto.agenda;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * Janela que representa as informacoes de um perfil
 */
public class PerfilJDialog extends JDialog {

	private static final long serialVersionUID = 5236261010171395235L;

	private Perfil perfil;
	
	private JPanel panel;
	private JLabel nomeJLabel;
	private JTextField nomeJTextField;
	private JLabel emailJLabel;
	private JTextField emailJTextField;
	private JLabel descricaoJLabel;
	private JTextField descricaoJTextField;
	
	private JButton salvarJButton;
	
	/**
	 * Construtor
	 * @param perfil Perfil que o dialog representara
	 */
	PerfilJDialog(Perfil perfil) {
		super();
		this.perfil = perfil;
		
		GridBagConstraints labelConstraints = new GridBagConstraints();
		GridBagConstraints fieldConstraints = new GridBagConstraints();
		
		labelConstraints.gridx = 0;
		labelConstraints.gridwidth = 1;
		labelConstraints.gridheight = 1;
		labelConstraints.weightx = 0;
		labelConstraints.ipadx = 10;
		labelConstraints.fill = GridBagConstraints.HORIZONTAL;
		
		fieldConstraints.gridx = 1;
		fieldConstraints.gridwidth = 1;
		fieldConstraints.gridheight = 1;
		fieldConstraints.weightx = 0.5;
		fieldConstraints.fill = GridBagConstraints.HORIZONTAL;
		
		
		panel = (JPanel)getContentPane();
		panel.setLayout(new GridBagLayout());
		
		fieldConstraints.gridy = labelConstraints.gridy = 0;
		nomeJLabel = new JLabel("Nome:");
		panel.add(nomeJLabel, labelConstraints);
		nomeJTextField = new JTextField(perfil.getNome());
		panel.add(nomeJTextField, fieldConstraints);
		
		fieldConstraints.gridy = labelConstraints.gridy = 1;
		emailJLabel = new JLabel("Email:");
		panel.add(emailJLabel, labelConstraints);
		emailJTextField = new JTextField(perfil.getEmail());
		panel.add(emailJTextField, fieldConstraints);
		
		fieldConstraints.gridy = labelConstraints.gridy = 2;
		descricaoJLabel = new JLabel("Descricao:");
		panel.add(descricaoJLabel, labelConstraints);
		descricaoJTextField = new JTextField(perfil.getDescricao());
		panel.add(descricaoJTextField, fieldConstraints);
		
		salvarJButton = new JButton("Salvar");
		salvarJButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				salvar();
			}
		});
		
		GridBagConstraints buttonConstraints = fieldConstraints;
		buttonConstraints.gridy = 3;
		buttonConstraints.weightx = 0;
		buttonConstraints.fill = GridBagConstraints.EAST;
		buttonConstraints.insets = new Insets(10, 0, 0, 10);
		buttonConstraints.anchor = GridBagConstraints.EAST;
		
		panel.add(salvarJButton, buttonConstraints);
		
		setTitle("Perfil");
		setModal(true);
		setSize(350, 150);
		setResizable(false);
		setVisible(true);
	}
	
	private void salvar() {
		perfil.setDescricao(descricaoJTextField.getText());
		perfil.setEmail(emailJTextField.getText());
		perfil.setNome(nomeJTextField.getText());
		dispose();
	}
}
